import HeadPrefab from "../../../component/HeadPrefab";
import DateUtil from "../../../engine/DateUtil";
import { EngineConfig } from "../../../engine/EngineConfig";
import { Logger } from "../../../engine/Logger";
import ViewBase from "../../../engine/ViewBase";
import { ClubSocketClient } from "../../../net/ClubSocketClient";
import { HttpHelper } from "../../../net/HttpHelper";
import { UserModel } from "../../model/UserModel";
import { UIHelper } from "../../util/UIHelper";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewClubTop extends ViewBase {

    private txtInputUserid: cc.EditBox
    private txtDate: cc.Label
    private showDate: number = 0;
    private topTypeToggle: cc.ToggleContainer;
    private defaultItemNode: cc.Node;

    private txtPage: cc.Label;
    private page_size: number = 10;
    private cur_page: number = 1;
    private totalPage: number = 1
    private totalNum: number = 0;

    private dataList: Array<object> = []

    onEnable(): void {
        super.onEnable()
        this.showDate = DateUtil.now()
        this.defaultItemNode = this.getChild(this.node, "scrollView/view/content/itemNode")
        this.txtInputUserid = this.getChild(this.node, "txtInputUserid", cc.EditBox);
        this.txtDate = this.getChild(this.node, "txtDate", cc.Label)
        this.topTypeToggle = this.getChild(this.node, "topTypeToggle", cc.ToggleContainer)
        let btnSearch: cc.Node = this.findChild(this.node, "btnSearch")
        UIHelper.onAddEvent(btnSearch, () => {
            this.onClickSearch(parseInt(this.txtInputUserid.string))
        })
        let btnPreDate: cc.Node = this.findChild(this.node, "btnPreDate")
        UIHelper.onAddEvent(btnPreDate, () => {
            this.cur_page = 1
            this.showDate -= EngineConfig.OneDay;
            this.getClubTop();
            this.refresh()
        })
        let btnNextDate: cc.Node = this.findChild(this.node, "btnNextDate")
        UIHelper.onAddEvent(btnNextDate, () => {
            this.showDate += EngineConfig.OneDay;
            this.cur_page = 1
            this.getClubTop();
            this.refresh();
        })

        this.txtPage = this.getChild(this.node, "txtPage", cc.Label);
        let btnPre: cc.Node = this.findChild(this.node, "btnPre")
        UIHelper.onAddEvent(btnPre, () => {
            if (this.cur_page > 1) {
                this.cur_page--;
                this.getClubTop();
            }
        })
        let btnNext: cc.Node = this.findChild(this.node, "btnNext")
        UIHelper.onAddEvent(btnNext, () => {
            if (this.cur_page < this.totalPage) {
                this.cur_page++
                this.getClubTop();
            }
        })
        this.refresh()
        this.getClubTop()
    }

    private onClickSearch(userid: number) {
        let showData: object = null
        for (let i = 0; i < this.dataList.length; i++) {
            let data: object = this.dataList[i]
            if (data["userid"] == userid) {
                showData = data
                break;
            }
        }
        if (showData) {
            this.refresh(showData)
        }
        else {
            HttpHelper.get_club_top_one(UserModel.instance.nowClub.clubid, userid, DateUtil.formatDateStr3(this.showDate), (data: object) => {
                if (data) {
                    this.refresh(data[0])
                }
            })
        }
    }

    private onTopTypeChange() {
        Logger.log("onTopTypeChange=")
        this.getClubTop()
    }

    private getClubTop() {
        let topType: number = this.getToggleContainerIndex(this.topTypeToggle) + 1
        HttpHelper.get_club_top(UserModel.instance.nowClub.clubid, topType, DateUtil.formatDateStr3(this.showDate), this.page_size, this.cur_page, (data: object) => {
            this.totalNum = data["totalNum"]
            this.dataList = data["data"]
            this.refresh();
        })
    }

    private refresh(showData: object = null) {
        this.txtDate.string = DateUtil.formatDateStr3(this.showDate)
        this.totalPage = this.totalNum % this.page_size == 0 ? this.totalNum / this.page_size : Math.floor(this.totalNum / this.page_size) + 1;
        if (this.totalPage <= 0) {
            this.totalPage = 1;
        }
        this.txtPage.string = this.cur_page + "/" + this.totalPage;
        this.hideAllChildren(this.defaultItemNode.parent)
        if (showData) {
            for (let i = 0; i < 1; i++) {
                let item: cc.Node = this.defaultItemNode.parent.children[i]
                if (!item) {
                    item = cc.instantiate(this.defaultItemNode)
                    item.parent = this.defaultItemNode.parent;
                }
                this.initOneItem(showData, item, i)
            }
        } else {
            for (let i = 0; i < this.dataList.length; i++) {
                let item: cc.Node = this.defaultItemNode.parent.children[i]
                if (!item) {
                    item = cc.instantiate(this.defaultItemNode)
                    item.parent = this.defaultItemNode.parent;
                }
                let data: object = this.dataList[i]
                this.initOneItem(data, item, i)
            }
        }
    }

    private initOneItem(data: object, item: cc.Node, index: number) {
        item.active = true;
        let topIndex: number = (this.cur_page - 1) * this.page_size + index + 1
        let txtTop: cc.Label = this.getChild(item, "txtTop", cc.Label)
        txtTop.string = topIndex + '';
        let head: HeadPrefab = this.getChild(item, "head", HeadPrefab)
        head.setHead(data["headimg"], data["lv"])
        let txtName: cc.Label = this.getChild(item, "txtName", cc.Label);
        txtName.string = data["nick_name"]
        let txtId: cc.Label = this.getChild(item, "txtId", cc.Label)
        txtId.string = "ID:" + data["userid"]
        let txtGameTimes: cc.Label = this.getChild(item, "txtGameTimes", cc.Label)
        txtGameTimes.string = data["gametimes"] + ""
        let txtVip: cc.Label = this.getChild(item, "txtVip", cc.Label)
        if (data["invite"]) {
            txtVip.string = data["invite"]
        } else {
            txtVip.string = "无VIP码"
        }
        let txtWinTimes: cc.Label = this.getChild(item, "txtWinTimes", cc.Label)
        txtWinTimes.string = data["wintimes"]
        let txtScore: cc.Label = this.getChild(item, "txtScore", cc.Label)
        txtScore.string = data["score"] + ""
    }


}
